import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

const routes = [{
    path: '/',
    name: 'index',
    component: () => import( /* webpackChunkName: "about" */ '../views/home.vue'),
    children: [
      //用户管理
      { //右侧内容
        path: "users",
        name: 'users',
        component: () => import('../views/users.vue')
      },
      //角色列表
      {
        path: '/roles',
        name: 'roles',
        component: () => import('../views/roles.vue')
      },
      //权限列表
      {
        path: '/rights',
        name: 'rights',
        component: () => import('../views/rights.vue')
      },
      //权限列表
      {
        path: '/goods',
        name: 'goods',
        component: () => import('../views/goods.vue')
      },
      //分类参数
      {
        path: '/params',
        name: 'params',
        component: () => import('../views/params.vue')
      },
      //商品分类
      {
        path: '/categories',
        name: 'categories',
        component: () => import('../views/categories.vue')
      },
      //订单列表
      {
        path: '/orders',
        name: 'orders',
        component: () => import('../views/orders.vue')
      },
      //数据报表
      {
        path: '/reports',
        name: 'reports',
        component: () => import('../views/reports.vue')
      }
    ]
  },
  { //登录
    path: '/login',
    name: 'login',
    component: () => import( /* webpackChunkName: "about" */ '../views/login.vue')
  },
  { //内容详情页面
    path: '/home',
    name: 'home',
    component: () => import( /* webpackChunkName: "about" */ '../views/home.vue')
  }
]

const router = new VueRouter({
  routes
})



//守卫导航
// to 到哪里去
// from 是从那里来
router.beforeEach((to, from, next) => {
  /*  console.log(to); //即将要进入的路由对象
  console.log(from); //当前导航正要离开的地方 */
  if (to.path == '/home' || to.path == '/') {
    if (!localStorage.getItem('userinfo')) {
      next('/login')
    }
  }
  next()
  if (to.path == '/login') {
    if (localStorage.getItem('userinfo')) {
      next('/home')
    }
  }
  next()
})
export default router